/**
 *      1.双重if,降低锁竞争频率
 *      2.给instance加上volatile,防止内存可见性问题
 */
class SingletonLazy3 {
    private static Object locker = new Object();
    private static volatile SingletonLazy3 instance = null;

    public static SingletonLazy3 getInstance() {
        if(instance == null) {
            synchronized (locker) {
                if(instance == null) {
                    instance = new SingletonLazy3();
                }
            }
        }
        return instance;
    }
    private SingletonLazy3() {}
}
//对懒汉模式进行加锁
class SingletonLazy2 {
    private static Object locker = new Object();
    private static  SingletonLazy2 instance = null;

    public static SingletonLazy2 getInstance() {
        synchronized (locker) {
            if(instance == null) {
                instance = new SingletonLazy2();
            }
            return instance;
        }
    }
    private SingletonLazy2() {}
}
//单线程->懒汉模式,线程不安全(多线程情况下,会创建多个实例)
class SingletonLazy {
    private static SingletonLazy instance = null;
    public static SingletonLazy getInstance() {
        if(instance == null) {
            instance = new SingletonLazy();
        }
        return instance;
    }
    //private构造方法
    private SingletonLazy() {}
}
//单例模式->饿汉模式,单线程,类创建的时候加载实例
class Singleton{
    private static SingletonLazy instance = null;
    public static SingletonLazy getInstance() {
        return instance;
    }
    //private构造方法
    private Singleton() {}
}
public class Test {
    /**
     * 认识线程
     *      8.多线程案例
     *
     */
    /**
     * 多线程案例
     *      1.单例模式
     */
    /**
     * 单例模式->保证某个类在程序中只存在唯一一份实例,而不会创建多个实例
     *      1.饿汉模式
     *      2.懒汉模式
     */
    public static void main(String[] args) {
        
    }
}
